<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="订单类型: 0-商品订单 1-产业中心-会议订单家 2-产业中心-展会订单 3-产业中心-沙龙订单 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderType">
              <j-dict-select-tag type="list" v-model="model.orderType" dictCode="order_type" placeholder="请选择订单类型: 0-商品订单 1-产业中心-会议订单家 2-产业中心-展会订单 3-产业中心-沙龙订单 " disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否拆分（0-未拆分 1-拆分）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSplitted">
              <j-dict-select-tag type="list" v-model="model.isSplitted" dictCode="order_splitted" placeholder="请选择是否拆分（0-未拆分 1-拆分）" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单状态：0-待支付、1-待发货、2-待收货、3-已完成 、4-已取消" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderStatus">
              <j-dict-select-tag type="list" v-model="model.orderStatus" dictCode="order_status" placeholder="请选择订单状态：0-待支付、1-待发货、2-待收货、3-已完成 、4-已取消" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="父订单ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
              <a-input v-model="model.parentId" placeholder="请输入父订单ID" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderSn">
              <a-input v-model="model.orderSn" placeholder="请输入订单编号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponId">
              <a-input v-model="model.couponId" placeholder="请输入优惠券ID" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentId">
              <a-input v-model="model.paymentId" placeholder="请输入支付ID" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="买家ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyerId">
              <a-input v-model="model.buyerId" placeholder="请输入买家ID" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发货地址ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addressId">
              <a-input v-model="model.addressId" placeholder="请输入发货地址ID"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品总数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalQuantity">
              <a-input-number v-model="model.totalQuantity" placeholder="请输入商品总数量" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品总金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalAmount">
              <a-input-number v-model="model.totalAmount" placeholder="请输入商品总金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总运费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalFreight">
              <a-input-number v-model="model.totalFreight" placeholder="请输入总运费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠总金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalDiscountAmount">
              <a-input-number v-model="model.totalDiscountAmount" placeholder="请输入优惠总金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付总金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalPaymentAmount">
              <a-input-number v-model="model.totalPaymentAmount" placeholder="请输入支付总金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单留言" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveMessage">
              <a-textarea v-model="model.leaveMessage" rows="4" placeholder="请输入订单留言" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'HTOrderForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            orderType:0,
            isSplitted:0,
            orderStatus:0,
            parentId:"0",
            totalQuantity:1,
            totalAmount:0,
            totalFreight:0,
            totalDiscountAmount:0,
            totalPaymentAmount:0,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           orderType: [
              { required: true, message: '请输入订单类型: 0-商品订单 1-产业中心-会议订单家 2-产业中心-展会订单 3-产业中心-沙龙订单 !'},
           ],
           isSplitted: [
              { required: true, message: '请输入是否拆分（0-未拆分 1-拆分）!'},
           ],
           orderStatus: [
              { required: true, message: '请输入订单状态：0-待支付、1-待发货、2-待收货、3-已完成 、4-已取消!'},
           ],
           parentId: [
              { required: true, message: '请输入父订单ID!'},
           ],
           orderSn: [
              { required: true, message: '请输入订单编号!'},
           ],
           paymentId: [
              { required: true, message: '请输入支付ID!'},
           ],
           buyerId: [
              { required: true, message: '请输入买家ID!'},
           ],
           addressId: [
              { required: true, message: '请输入发货地址ID!'},
           ],
           totalQuantity: [
              { required: true, message: '请输入商品总数量!'},
           ],
           totalAmount: [
              { required: true, message: '请输入商品总金额!'},
           ],
           totalFreight: [
              { required: true, message: '请输入总运费!'},
           ],
           totalDiscountAmount: [
              { required: true, message: '请输入优惠总金额!'},
           ],
           totalPaymentAmount: [
              { required: true, message: '请输入支付总金额!'},
           ],
        },
        url: {
          add: "/order/hTOrder/add",
          edit: "/order/hTOrder/edit",
          queryById: "/order/hTOrder/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>